/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package generator;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javaapplication1.SQLiteParser;
import net.db.generator.BaseListModel;
import net.db.generator.field.models.DataBaseColumnType;
import net.etfbl.jgroky.jdbc.ConnectionPool;

/**
 *
 * @author abosancic
 */
public class SQLiteClassGenerator extends ClassGenerator{
    
    @Override
    protected void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        dbTablesList.removeAll();
        dbColumnsList.removeAll();
        dbList.setModel(new BaseListModel(new String[]{"sqlite_master"}));
    }
    
    @Override
    protected void dbListMouseClicked(java.awt.event.MouseEvent evt) {                                    
        //System.out.println("schemas: " + dbList.getSelectedValue());
//        if(dbList.getSelectedValue() != null){
//            dbColumnsList.setModel(new BaseListModel(new String[]{}));
//            dbGenList.setModel(new BaseListModel(new String[]{}));
//            dbTablesList.setModel(new BaseListModel(getAllTables(dbList.getSelectedValue().toString())));
//        }
    }   
    
//    @Override
//    protected String[] getAllTables(String schema) {
//       
//        ArrayList<String> allTables = new ArrayList<>();
//        
//        try {
//            ConnectionPool cp = ConnectionPool.getConnectionPool();
//            Connection conn = cp.checkOut();     
//            try {
//
//                ResultSet res = conn.createStatement().executeQuery("SELECT * FROM " +
//                                             " " + schema + " " +
//                                             "WHERE type='table'" +
//                                             "ORDER BY name");
//            while (res.next()) {
//                   String tableName = res.getString(2);
//                   allTables.add(tableName);
//            }
//
//            } catch (SQLException ex) {
//                Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
//            }
//            cp.checkIn(conn);
//
//        } catch (Exception ex) {
//            Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        
//        return allTables.toArray(new String[allTables.size()]);
//    }
    
//    @Override
//    protected String[] getAllColumns(String schema, String tabel) {
//        ArrayList<DataBaseColumnType> allColumns = new ArrayList<>();
//        try {
//            ConnectionPool cp = ConnectionPool.getConnectionPool();
//            Connection conn = cp.checkOut();     
//            try {
//                ResultSet rsColumns = null;
//                DatabaseMetaData meta = conn.getMetaData();
//                rsColumns = meta.getColumns(schema, null, tabel, null);
//                ResultSet pkColumns = meta.getPrimaryKeys(schema, null, tabel);
//                while (rsColumns.next()) {
//                    String columnName = rsColumns.getString("COLUMN_NAME");
//                    //System.out.println("column name=" + columnName);
//                    //String columnType = rsColumns.getString("TYPE_NAME");
//                    //System.out.println("type:" + columnType);
//                    String dataType = rsColumns.getString("DATA_TYPE");
//                    //System.out.println("data type:" + dataType);
//                    //String columnType1 = SQLTypeMap.convert(new Integer(dataType));
//                    //System.out.println("java data type:" + columnType1);
//                    int size = rsColumns.getInt("COLUMN_SIZE");
//                    //System.out.println("size:" + size);
//                    DataBaseColumnType ct = new DataBaseColumnType(columnName, dataType, size);
//                    //int nullable = rsColumns.getInt("NULLABLE");
//                    //if (nullable == DatabaseMetaData.columnNullable) {
//                    //    System.out.println("nullable true");
//                    //} else {
//                    //    System.out.println("nullable false");
//                    //}
//                    //int position = rsColumns.getInt("ORDINAL_POSITION");
//                    //System.out.println("position:" + position);
//                    allColumns.add(ct);
//                }
//            } catch (SQLException ex) {
//                Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
//            }
//            cp.checkIn(conn);
//
//        } catch (Exception ex) {
//            Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        
//        ArrayList<String> tmp = new ArrayList<>();
//        for (DataBaseColumnType type : allColumns) {
//            tmp.add(type.toString());
//        }
//        
//        return tmp.toArray(new String[tmp.size()]);
//    }
    
//    protected String[] getAllColumns(String schema, String tabel) {
//        ArrayList<DataBaseColumnType> allColumns = new ArrayList<>();
//        try {
//            ConnectionPool cp = ConnectionPool.getConnectionPool();
//            Connection conn = cp.checkOut();     
//            try {
//                ResultSet rsColumns = null;
//                DatabaseMetaData meta = conn.getMetaData();
//                rsColumns = meta.getColumns(schema, null, tabel, null);
//                
//                 ResultSet res = conn.createStatement().executeQuery("SELECT * FROM " +
//                                             " sqlite_master " +
//                                             "WHERE type='table' " + "AND name = 'drzava'"
//                                             );
//            
//            String ra = "";
//            while (res.next()) {
//                    ra = res.getString(5);
//                }
//            System.out.println("" + new SQLiteParser(ra));
//                
//                while (rsColumns.next()) {
//                    String columnName = rsColumns.getString("COLUMN_NAME");
//                    //System.out.println("column name=" + columnName);
//                    //String columnType = rsColumns.getString("TYPE_NAME");
//                    //System.out.println("type:" + columnType);
//                    String dataType = rsColumns.getString("DATA_TYPE");
//                    //System.out.println("data type:" + dataType);
//                    //String columnType1 = SQLTypeMap.convert(new Integer(dataType));
//                    //System.out.println("java data type:" + columnType1);
//                    int size = rsColumns.getInt("COLUMN_SIZE");
//                    //System.out.println("size:" + size);
//                    DataBaseColumnType ct = new DataBaseColumnType(columnName, dataType, size);
//                 
//                    allColumns.add(ct);
//                }
//            } catch (SQLException ex) {
//                Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
//            }
//            cp.checkIn(conn);
//
//        } catch (Exception ex) {
//            Logger.getLogger(ClassGenerator.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        
//        ArrayList<String> tmp = new ArrayList<>();
//        for (DataBaseColumnType type : allColumns) {
//            tmp.add(type.toString());
//        }
//        
//        return tmp.toArray(new String[tmp.size()]);
//    }
     /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ClassGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ClassGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ClassGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ClassGenerator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SQLiteClassGenerator().setVisible(true);
            }
        });
    }
}
